package com.hudson.plugin.server.hibernateDB.internal.domain.dao;

import com.hudson.plugin.server.hibernateDB.domain.dao.PromotionsDAOInterface;
import com.hudson.plugin.server.hibernateDB.internal.domain.entity.PromotionsEntity;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * Promotions entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see .Promotions
 */

public class PromotionsDAO extends HibernateDaoSupport implements PromotionsDAOInterface {
    private static final Logger log = LoggerFactory
            .getLogger(PromotionsDAO.class);

    protected void initDao() {
        // do nothing
    }

    public void save(PromotionsEntity transientInstance) {
        log.debug("saving Promotions instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(PromotionsEntity persistentInstance) {
        log.debug("deleting Promotions instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public PromotionsEntity findById(Integer id) {
        log.debug("getting Promotions instance with id: " + id);
        try {
            PromotionsEntity instance = (PromotionsEntity) getHibernateTemplate().load(
                    PromotionsEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(PromotionsEntity instance) {
        log.debug("finding Promotions instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: "
                    + results.size());
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Promotions instance with property: " + propertyName
                + ", value: " + value);
        try {
            String queryString = "from PromotionsEntity as model where model."
                    + propertyName + " = ?";
            List results = getHibernateTemplate().find(queryString, value);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByName(Object name) {
        return findByProperty(NAME, name);
    }

    public List findAll() {
        log.debug("finding all Promotions instances");
        try {
            String queryString = "from PromotionsEntity";
            List results = getHibernateTemplate().find(queryString);
            if (results == null) {
                return new ArrayList();
            }
            return results;
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public PromotionsEntity merge(PromotionsEntity detachedInstance) {
        log.debug("merging Promotions instance");
        try {
            PromotionsEntity result = (PromotionsEntity) getHibernateTemplate().merge(
                    detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(PromotionsEntity instance) {
        log.debug("attaching dirty Promotions instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(PromotionsEntity instance) {
        log.debug("attaching clean Promotions instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public static PromotionsDAOInterface getFromApplicationContext(ApplicationContext ctx) {
        return (PromotionsDAOInterface) ctx.getBean("PromotionsDAO");
    }
}